<?php
namespace App\Controllers\Home\Page;
use \App\Libs\Home\Home;
use \App\Libs\Helper\WeixinHelper as Weixin;
use \App\Models\Home\User;

class Select extends  \App\Libs\Base\BaseTwigAction 
{
    protected function beforeRun() {
        parent::beforeRun();
        //$this->needLogin = TRUE;
    }
	
	public function execute(){
		$openid = $this->request->get('openid');
		$key = '';
		if (isset($_COOKIE[SESSION_KEY_OPEN]) && ! empty($_COOKIE[SESSION_KEY_OPEN])) {
            $key = trim($_COOKIE[SESSION_KEY_OPEN]);
		}
		if( empty($key) && !\App\Libs\Cache\Redis::get(trim($key)) ){
			if( empty( $openid ) ){
			 	header("Location:http://www.zgszysg.com/weixin/oauthInfoLiantongs.html");
			}
			$userInfo['openid'] = $openid;
			\App\Libs\Cache\Redis::setex($userInfo['openid'], 3600*24*30, $userInfo['openid']);
			setcookie(SESSION_KEY_OPEN, $userInfo['openid'], time() + 7200, '/', COOKIE_DOMAIN);
		}else{
			$openid =	\App\Libs\Cache\Redis::get(trim($key));
		}
		$user = User::find(array(
			' openid = :openid: and is_user != 0',
			'bind'=>array( 'openid'=>$openid )
		));
		$is_user = 0;
		if( empty( $user ) ){
			$user = new user();
			$user->openid = $openid;
			$user->ctime = date('Y-m-d H:i:s');
			$user->utime = date('Y-m-d H:i:s');
			$user->save();
		}else{
			//header('Location:/home/1.html');
			$is_user = $user->is_user;
		}
		$val = \App\Libs\Helper\WeixinHelper::getSignature();
		$this->addParam('weixin',$val);
		$this->addParam('is_user',$is_user);
		
		return parent::execute();
	}

}
